package org.soarsky.wealthchengdu.module.set;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.entity.NewsContentData;
import org.soarsky.wealthchengdu.entity.NewsData;
import org.soarsky.wealthchengdu.imageCache.CacheMemUtil;
import org.soarsky.wealthchengdu.imageCache.ImageLoader;
import org.soarsky.wealthchengdu.imageCache.ImageLoader.ImageCallback;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CollectListAdapter extends BaseAdapter {
	private Context context;
	private List<NewsContentData> listdata;

	public CollectListAdapter(Context context) {
		this.context = context;
		listdata = new ArrayList<NewsContentData>();
	}

	public void setlistdata(List<NewsContentData> listdata) {
		this.listdata = listdata;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listdata.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HolderView holderview = null;
		if (convertView == null) {
			holderview = new HolderView();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.cd_list_det, null);
			holderview.cd_list_tv_name = (TextView) convertView
					.findViewById(R.id.cd_list_tv_name);
			holderview.tv_text = (TextView) convertView
					.findViewById(R.id.tv_text);
			holderview.cd_tv_zan = (TextView) convertView
					.findViewById(R.id.cd_tv_zan);
			holderview.cd_tv_chat = (TextView) convertView
					.findViewById(R.id.cd_tv_chat);
			holderview.cd_tv_collectss = (TextView) convertView
					.findViewById(R.id.cd_tv_collectss);
			holderview.cd_tv_share = (TextView) convertView
					.findViewById(R.id.cd_tv_share);
			holderview.cd_iv_imgs = (ImageView) convertView
					.findViewById(R.id.cd_iv_imgs);
			holderview.cd_tv_data = (TextView) convertView
					.findViewById(R.id.cd_tv_data);
			convertView.setTag(holderview);
		} else {
			holderview = (HolderView) convertView.getTag();
		}

		if (MyAppliction.mCn == 0) {
			holderview.cd_list_tv_name.setText(listdata.get(position)
					.getTitle());
			holderview.tv_text.setText(listdata.get(position).getSubTitle());

		} else {
			holderview.cd_list_tv_name.setText(listdata.get(position)
					.getTitleEn());
			holderview.tv_text.setText(listdata.get(position).getSubTitleEn());

		}
		holderview.cd_tv_data.setText(getStrTime(listdata.get(position)
				.getAddTime()));
		holderview.cd_tv_zan.setText(listdata.get(position).getPvNum());
		holderview.cd_tv_chat.setText(listdata.get(position).getRemarkNum());
		holderview.cd_tv_share.setText(listdata.get(position).getShareNum());
		holderview.cd_tv_collectss.setText(listdata.get(position)
				.getCollectNum());
		loadImage(listdata.get(position).getImg(), holderview.cd_iv_imgs);
		// holderview.cd_iv_imgs.setText(listdata.get(position).getShareNum());
		return convertView;
	}

	class HolderView {
		TextView cd_list_tv_name, tv_text, cd_tv_zan, cd_tv_chat, cd_tv_share,
				cd_tv_collectss, cd_tv_data;
		ImageView cd_iv_imgs;
	}

	// 这个方法还必须写在adapter里面
	private void loadImage(final String url, final ImageView image) {

		// Bitmap bm = CacheMemUtil.getImage(url);
		Bitmap bm = CacheMemUtil.getImageFromCache(url); // 第一步，仅仅只从内存中读取
		if (bm != null) {
			// 包括从本地获取的，未进行图片处理的图片
			// bm = ImageUtil.zoomBitmap(bm, iconWidth, iconHeight);// 缩放处理
			// bm = ImageUtil.getRoundedCornerBitmap(bm, corners);// 圆角处理
			// CacheMemUtil.putImage2Cache(url, bm);

			image.setImageBitmap(bm);
		} else {
			image.setImageResource(R.drawable.loading);

			ImageLoader.getInstance().loadBitmap(url, new ImageCallback() {

				public void imageLoaded(Bitmap bm) {
					if (bm != null) {
						// //以下两行，根本不该在这里调用，应该在存在本地之前，而不是读取显示时才做处理
						// bm = ImageUtil.zoomBitmap(bm, iconWidth,
						// iconHeight);// 缩放处理
						// bm = ImageUtil.getRoundedCornerBitmap(bm, corners);//
						// 圆角处理
						CacheMemUtil.putImage2Cache(url, bm);
						notifyDataSetChanged();
						// image.setImageBitmap(bm); //不能使用这句方法，否则会在加载完之前错误显示
					}
				}
			});
		}
	}

	// 将时间戳转为字符串
	public static String getStrTime(String cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
		// 例如：cc_time=1291778220
		long lcc_time = Long.valueOf(cc_time);
		re_StrTime = sdf.format(new Date(lcc_time * 1000L));
		return re_StrTime;
	}
}
